package com.server.common.redis.service.verify;

import com.server.common.code.error.BusinessException;
import com.server.common.redis.constant.RedisKeysConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Component
public class RedisLoginVerifyService {


    @Resource
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 校验登录验证码
     * @author LXC
     * @date 2023/2/2 16:23
     */
    public void checkLoginVerifyCode(String textCode, String textValue){
//        //暂不使用
//        String textCodeKey = RedisKeysConstant.VERIFY_LOGIN_CODE +textCode;
//        String verifyCode = redisTemplate.opsForValue().get(textCodeKey);
//        redisTemplate.expire(textCodeKey,0,TimeUnit.SECONDS);
//
//        if(ObjectUtils.isEmpty(verifyCode)){
//            throw new BusinessException("验证码失效!");
//        }else if(!verifyCode.equalsIgnoreCase(textValue)){
//            throw new BusinessException("验证码输入错误!");
//        }
    }

    /**
     * 添加登录验证码
     * @author LXC
     * @date 2023/2/2 16:23
     */
    public void addLoginVerify(String textCode,String textValue){
        redisTemplate.opsForValue().set(RedisKeysConstant.VERIFY_LOGIN_CODE + textCode,textValue,
                3*60L,TimeUnit.SECONDS);
    }

    /**
     * 删除登录验证码
     * @author LXC
     * @date 2023/2/2 16:23
     */
    public void delLoginVerify(String textCode){
        redisTemplate.expire(RedisKeysConstant.VERIFY_LOGIN_CODE + textCode,
                0L, TimeUnit.SECONDS);
    }
}
